﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Aribaccio;

namespace BDPAutodetect.Models
{
    /// <summary>
    /// 参数配置数据
    /// </summary>
    public partial class MParameterData : ObservableModel
    {
        /// <summary>
        /// 标识/编号
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(NameDisplay))]
        protected string name;

        /// <summary>
        /// 从机设备实例
        /// </summary>
        [ObservableProperty]
        protected MSlave slave;

        /// <summary>
        /// 是否勾选
        /// </summary>
        [ObservableProperty]
        protected bool isChecked;

        /// <summary>
        /// 界限数据集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MParameterLimitData> limits;

        /// <summary>
        /// 是否有效存在界限信息
        /// </summary>
        public bool IsValidLimit => this.Limits != null && this.Limits.Count > 0;
        /// <summary>
        /// 名称显示
        /// </summary>
        public string NameDisplay => string.Concat("参数", ID, ":", Name);

        /// <summary>
        /// 构造函数
        /// </summary>
        public MParameterData(int argID, string argName, MSlave argSlave)
        {
            this.ID = argID;
            this.Name = argName;
            this.Slave = argSlave;
            this.IsChecked = false;
            this.Limits = new ObservableCollection<MParameterLimitData>();
        }

        /// <summary>
        /// 深度克隆
        /// </summary>
        /// <returns></returns>
        public MParameterData CloneDeep()
        {
            var pData = new MParameterData(this.ID, this.Name, this.Slave)
            {
                IsChecked = this.IsChecked
            };
            foreach (var item in this.Limits)
            {
                pData.Limits.Add(item.CloneDeep());
            }
            return pData;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public TParameter CloneTData()
        {
            return new TParameter()
            {
                ID = this.ID,
                SlaveID = this.Slave.ID,
                Name = this.Name
            };
        }
    }
}
